﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using JmShared;

namespace JiveMessenger.Models
{
    public class JiveChatGroupModel : IEquatable<JiveChatGroupModel>, IChatGroup, INotifyPropertyChanged
    {
        public string ID { get; private set; }


        private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    RaisePropertyChanged("Name");
                }
            }
        }

        private bool _isBlocked;
        public bool IsBlocked
        {
            get { return _isBlocked; }
            set
            {
                if (_isBlocked != value)
                {
                    _isBlocked = value;
                    RaisePropertyChanged("IsBlocked");
                }
            }
        }

        private Dictionary<string, GroupPrivilege> _groupMembers;
        public Dictionary<string, GroupPrivilege> GroupMembers
        {
            get { return _groupMembers; }
            set
            {
                if (_groupMembers != value)
                {
                    _groupMembers = value;
                    RaisePropertyChanged("GroupMembers");
                }
            }
        }

        private bool _isExpanded;
        public bool IsExpanded
        {
            get
            {
                return IsBlocked ? false : _isExpanded;
            }
            set
            {
                if (_isExpanded != value)
                {
                    _isExpanded = value;
                    RaisePropertyChanged("IsExpanded");
                }
            }
        }

        public GroupType Type
        {
            get { return GroupType.JiveChatGroup; }

        }

        public JiveChatGroupModel(string id, string name)
        {
            this.ID = id;
            this.Name = name;
            this.GroupMembers = new Dictionary<string, GroupPrivilege>();
        }

        public JiveChatGroupModel(string id, string name, Dictionary<string, GroupPrivilege> groupMembers)
        {
            this.ID = id;
            this.Name = name;
            this.GroupMembers = groupMembers;
            this.IsExpanded = true;
        }

        public override bool Equals(object obj)
        {
            return Equals(obj as JiveChatGroupModel);
        }

        public override int GetHashCode()
        {
            return this.ID.GetHashCode();
        }

        public bool Equals(JiveChatGroupModel other)
        {
            if (other == null)
                return false;
            return this.ID == other.ID;
        }

        protected virtual void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }
}
